package task;

import handingIn.HandingIn;
import lecture.LectureManagedBean;
import com.icesoft.faces.context.FileResource;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import org.icefaces.component.fileentry.FileEntry;
import org.icefaces.component.fileentry.FileEntryEvent;
import org.icefaces.component.fileentry.FileEntryResults;
import org.icefaces.component.fileentry.FileEntryResults.FileInfo;

/**
 *
 * @author Thorsten Ludwig
 */

@ManagedBean(name="newSheetBean")
@CustomScoped(value="#{window}")
public class NewSheetManagedBean {
   
    private ExerciseSheet sheet;
    
    private boolean isExerciseUploaded;
    
    private boolean isSolutionUploaded;
    
    private int numExercises;
    
    private int numHandingIns;
    
    @ManagedProperty(value="#{lectureBean}")
    private LectureManagedBean lectureBean;
    
    public NewSheetManagedBean() {
        this.sheet = new ExerciseSheet();
    }

    public LectureManagedBean getLectureBean() {
        return lectureBean;
    }

    public void setLectureBean(LectureManagedBean lectureBean) {
        this.lectureBean = lectureBean;
    }
    
    

    public boolean isIsExerciseUploaded() {
        return isExerciseUploaded;
    }

    public void setIsExerciseUploaded(boolean isExerciseUploaded) {
        this.isExerciseUploaded = isExerciseUploaded;
    }

    public boolean isIsSolutionUploaded() {
        return isSolutionUploaded;
    }

    public void setIsSolutionUploaded(boolean isSolutionUploaded) {
        this.isSolutionUploaded = isSolutionUploaded;
    }

    public void solutionUploadListener(FileEntryEvent event) {
        FileEntry fileEntry = (FileEntry) event.getSource();
        FileEntryResults results = fileEntry.getResults();
        FileResource fileResource;
        for (FileInfo fileInfo : results.getFiles()) {
            if (fileInfo.isSaved()) {
                fileResource = new FileResource(fileInfo.getFile());
                this.sheet.setSolutionFile(fileResource.getFile());
                this.isSolutionUploaded = true;
            } 
        }
    }
    
    public void exerciseUploadListener(FileEntryEvent event) {
        System.out.println("Listener called");
        FileEntry fileEntry = (FileEntry) event.getSource();
        FileEntryResults results = fileEntry.getResults();
        System.out.println(fileEntry.getAbsolutePath());
        FileResource fileResource;
        for (FileInfo fileInfo : results.getFiles()) {
            if (fileInfo.isSaved()) {
                fileResource = new FileResource(fileInfo.getFile());
                this.sheet.setExerciseSheetFile(fileResource.getFile());
                System.out.println("File: " + fileInfo.getFile().getAbsolutePath());
                System.out.println("File Resource: " + fileResource);
                this.isExerciseUploaded = true;
            }
        }
    }
    
    public void valueChangeNumberSheets(ValueChangeEvent v) {
        try {
            int newVal = 0;
            if (v.getNewValue() != null || v.getNewValue().toString().equals(""))
                newVal = Integer.parseInt(v.getNewValue().toString());
                System.out.println("Neuer Wert für Sheets: " + newVal); 
                this.changeNumberExercises(newVal);
        }
        catch(NumberFormatException n) {
        }
    }
    
    public void valueChangeNumberHandingIns(ValueChangeEvent v) {
        try { 
            int newVal = 0;
            if (v.getNewValue() != null || v.getNewValue().toString().equals(""))
                newVal = Integer.parseInt(v.getNewValue().toString());
                System.out.println("Neuer Wert für Handing In: " + newVal); 
                this.changeNumberHandingIns(newVal);
        }
        catch (NumberFormatException n) {
            
        }
    }
    
    public String addSheet() {
        System.out.println("Add New Sheet Called with " + this.sheet.getExerciseSheetFileName());
        if (this.sheet.getExerciseSheetFileName().equals("")) {
            this.sheet.setExerciseSheetFileName("blatt.pdf");
        }
        
        
        lectureBean.getLecture().addSheet(sheet);
        this.sheet.getCategory().incrementCounter();
        
        this.sheet = new ExerciseSheet();
        this.isExerciseUploaded = false;
        this.isSolutionUploaded = false;        
        this.numExercises = 0;      
        this.numHandingIns = 0;
        
        return "showSheets.jsf";
    }

   
    
    
   private void changeNumberExercises(int number) {
        System.out.println("Change Number Exercise aufgerufen");
        while (number != this.sheet.getExercises().size()) {
            if (number > this.sheet.getExercises().size()) {
                System.out.println("Add Exercise");
                this.sheet.getExercises().add(new Task());
            } else {
                System.out.println("Remove Exercise");
                this.sheet.getExercises().remove(this.sheet.getExercises().size()-1);
            }
        }
    }
    
    private void changeNumberHandingIns(int number) {
        System.out.println("Change Handing In aufgerufen");
        while (number != this.sheet.getHandingIns().size()) {
            if (number > this.sheet.getHandingIns().size()) {
                HandingIn h = new HandingIn();
                h.setHandingInDate(this.sheet.getDateOfReturn());
                this.sheet.getHandingIns().add(h);
            } else {
                this.sheet.getHandingIns().remove(this.sheet.getHandingIns().size()-1);
            }
        }
    }

    public int getNumExercises() {
        return numExercises;
    }

    public void setNumExercises(int numExercises) {
        this.numExercises = numExercises;
    }

    public int getNumHandingIns() {
        return numHandingIns;
    }

    public void setNumHandingIns(int numHandingIns) {
        this.numHandingIns = numHandingIns;
    }

    public ExerciseSheet getSheet() {
        return sheet;
    }

    public void setSheet(ExerciseSheet sheet) {
        this.sheet = sheet;
    }
    
    public List<SelectItem> getCategoryItems() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture l = lb.getLecture();
        
        List<SelectItem> list  = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Keine Kategorie ausgewählt"));
        for (TaskCategory t: l.getExerciseCategories()) {
            list.add(new SelectItem(t, t.getName()));
        }
        
        return list;
    }
}